package com.tuo51.childcare.controller;

import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.evaluate.service.EvaluateV2Service;
import com.tuo51.core.service.StaffService;
import com.tuo51.foudation.constant.CommonConstant;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@Slf4j
@RequestMapping("/papi/profile")
@Api(value = "/papi/profile", tags = "家长服务号 - 用户信息")
public class PAPIUserProfileController {

    @Autowired
    MemVerificationCache cache;

    @Autowired
    StaffService staffService;

    @Autowired
    EvaluateV2Service evaluateV2Service;

    @ApiOperation("绑定家长手机号码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码"),
            @ApiImplicitParam(name = "verifyCode", value = "验证码")
    })
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public StatusResponse doBindDevice(HttpServletRequest req,
                                       HttpServletResponse resp,
                                       @RequestHeader(value = "ticket", required = false) String ticket,
                                       String mobile,
                                       String verifyCode){
        log.info("bind");

        //校验验证码
        if( (staffService.isTestMobile(mobile) && "123456".equals(verifyCode))
                || staffService.checkSmsCode(mobile, verifyCode)){
            staffService.clearSmsCode(mobile);

            Staff staff = cache.getStaff(ticket);

            staffService.updatePhoneNumber(staff, mobile);

            //更新feed列表
            evaluateV2Service.grantAllEvaluateToParent(staff);

            return StatusResponse.success();
        }else{
            return StatusResponse.error(ErrorCode.SMS_CODE_ERROR, "短信验证码错误");
        }
    }

    @ApiOperation("解绑家长手机号码")
    @RequestMapping(value = "/debind", method = RequestMethod.POST)
    public StatusResponse deBindUser(HttpServletRequest req,
                                     HttpServletResponse resp,
                                     @RequestHeader(value = "ticket", required = false) String ticket
    ){
        log.info("debind");

        Staff staff = cache.getStaff(ticket);

        //校验验证码
        staffService.updatePhoneNumber(staff, null);

        //更新feed列表
        evaluateV2Service.grantAllEvaluateToParent(staff);

        return StatusResponse.success();
    }


    @ApiOperation("发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码, 若系统内不存在手机号码对应的家长，则报错"),
    })
    @RequestMapping(value = "/send_bind_code",method = RequestMethod.POST)
    public Object actionCallBack(HttpServletRequest req,
                                 HttpServletResponse resp,
                                 @RequestHeader(value = "ticket", required = false) String ticket,
                                 String mobile){
        log.info("send_bind_code");

        if(staffService.isTestMobile(mobile) ){
            return StatusResponse.success();
        }

        boolean result = staffService.sendSms(mobile, CommonConstant.Platform.WXMP.codeOf());

        return StatusResponse.success(result);
    }



}
